// pages/mall/goodsDetail/goodsDetail.js
import { Api } from "../../../utils/api.js"
import { Http } from '../../../utils/http.js'
let http = new Http()
let app = getApp()

Page({

  /**
   * 页面的初始数据
   */
  data: {
    goods: null,
    showParameter: 0,
    isProxy: 0,
    showShareMenu: 0,
    shareIdToSell: 0,
    coupons: [],
    showAllParam: false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var scene = options.scene
    var shareId = scene == undefined || scene == 'undefined' ? options.shareId : scene
    var goodsId = options.goodsId
    if (shareId != undefined && shareId != null) {
      wx.setStorage({
        key: 'shareId', 
        data: shareId,
      })
    }
    this.getGoodsDetail(goodsId, shareId)
    this.getAllCoupons()
    this.getUserRole()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    var that = this
    return {
      title: '我推荐 | ' + this.data.goods.title,
      path: '/pages/mall/goodsDetail/goodsDetail?goodsId=' + this.data.goods.id + '&shareId=' + this.data.shareIdToSell,
      imageUrl: this.data.goods.share,
      success: (res) => {
        if (res.errMsg == 'shareAppMessage:ok') {
          that.setData({
            showShareMenu: 0
          })
        }
      },
      fail: (res) => {
        
      }
    }
  },

  /**
   * 自定义方法
   */
  getGoodsDetail: function (goodsId, shareId) {
    var that = this
    var data = {}
    if (goodsId != undefined) {
      data.goodsId = goodsId
    } else if (shareId != undefined) {
      data.shareId = shareId
    }
    http.sendHttp(Api.get_goods_detail, {
      data: data,
      success: function (res) {
        that.setData({
          goods: res.data.obj
        })
      }
    })
  },

  tapShowAll: function(){
    var showAllParam = this.data.showAllParam
    this.setData({
      showAllParam: !showAllParam
    })
  },

  getUserRole: function () {
    var that = this
    http.sendHttp(Api.get_user_role, {
      data: { },
      success: function (res) {
        that.setData({
          isProxy: res.data.obj.isProxy
        })
      }
    })
  },

  addToCart: function () {
    var that = this
    http.sendHttp(Api.add_goods_to_cart, {
      data: {
        goodsId: that.data.goods.id
      },
      success: function (res) {
        wx.showToast({
          title: '加入成功'
        })
      }
    }, 'POST')
  },

  showShareMenu: function () {
    var that = this
    this.getGoodsShare(function (shareId) {
      that.setData({
        showShareMenu: 1,
        shareIdToSell: shareId
      })
    })
  },
  closeShareMenu: function () {
    this.setData({
      showShareMenu: 0
    })
  },

  goSell: function () {
    var shareIdToSell = this.data.shareIdToSell
    wx.navigateTo({
      url: '/pages/mall/sell/sell?shareId=' + shareIdToSell + '&goods=' + JSON.stringify(this.data.goods),
    })
  },

  getGoodsShare: function (callback) {
    http.sendHttp(Api.get_goods_share, {
      data: {
        goodsId: this.data.goods.id
      },
      success: function (res) {
        callback(res.data.obj.shareId)
      }
    })
  },

  getAllCoupons: function () {
    var that = this
    http.sendHttp(Api.get_all_coupons, {
      data: {},
      success: function (res) {
        that.setData({
          coupons: res.data.obj.coupons
        })
      }
    })
  },

  goReceiveCoupons: function () {
    wx.navigateTo({
      url: '/pages/mall/goodsCoupons/goodsCoupons',
    })
  },

  doNothing: function () {

  },

  tapHome: function(){
    wx.reLaunch({
      url: "../index/index"
    })
  },

  tapCart: function(){
    wx.switchTab({
      url: "/pages/cart/index/index"
    })
  },

  buy: function(){
    var userToken = app.globalData.userToken
    if(userToken == null || userToken.length == 0){
      wx.showModal({
        title: '提示',
        content: '用户未登录',
        confirmText: '去登录',
        success: function (res) {
          if (res.confirm) {
            wx.navigateTo({
              url: '/pages/system/login/login',
            })
          }
        }
      })
    } else {
      var cart = [{
        goodsId: this.data.goods.id,
        goods: this.data.goods,
        priceAmount: this.data.goods.priceSell,
        number: 1
      }]
      wx.navigateTo({
        url: '/pages/mall/pay/pay?orderList=' + JSON.stringify(cart) + "&isBuy=1"
      })
    }
  },

  previewImage: function (e) {
    wx.previewImage({
      urls: [e.currentTarget.dataset.url]
    })
  }
})